// pages/sweepCode/sweepCode.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: '',
    overlayShow: false,
    zc: '',
    xz: '',
    needZc: '',
    needXz: '',
    moreXz: false,
    ddNumber: '',
    pwd: '',
    secondPayPassword: false,
    payFocus: true,
    whatpay: 0,
    msg: {}
  },
  // 绑定输入金钱
  moneychange(e) {
    this.setData({
      money: e.detail.value
    })
  },
  // 购买星钻
  buyxz() {
    wx.navigateTo({
      url: '../xingzuan/xingzuan',
    })
  },
  topay() {
    var that = this
    if (this.data.money == '') {
      wx.showToast({
        title: '请输入金额',
        icon: 'none'
      })
      return
    }
    wx.request({
      url: app.globalData.baseUrl + '/getCurrencyIntegralNumByCash',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        amount: this.data.money,
        merchantId: this.data.msg.id
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            needXz: res.data.data.currency,
            needZc: res.data.data.integral
          })
        }
      }
    })
    this.setData({
      overlayShow: true
    })
  },
  // 星钻支付选择
  xzpay() {
    var that = this
    if (this.data.needXz > this.data.xz) {
      wx.showToast({
        title: '星钻余额不足，请重新选择支付方式！',
        icon: 'none'
      })
      this.setData({
        moreXz: true
      })
    } else {
      wx.request({
        url: app.globalData.baseUrl + '/offlinePlaceOrder',
        data: {
          totalNum: that.data.money,
          merchantId: that.data.msg.id,
          payMethod: 2,
          currencyNum: that.data.needXz,
          xcxOpenId: app.globalData.xcxOpenId
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.code == 0) {
            that.setData({
              ddNumber: res.data.data,
              secondPayPassword: true,
              payFocus: true,
              whatpay: 2
            })
          }
        }
      })
    }
  },
  // zc支付选择
  zcpay() {
    var that = this
    if (this.data.needZc > this.data.zc) {
      wx.showToast({
        title: 'ZC余额不足，请重新选择支付方式！',
        icon: 'none'
      })
    } else {
      wx.request({
        url: app.globalData.baseUrl + '/offlinePlaceOrder',
        data: {
          totalNum: that.data.money,
          merchantId: that.data.msg.id,
          payMethod: 3,
          integralNum: that.data.needZc,
          xcxOpenId: app.globalData.xcxOpenId
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.code == 0) {
            that.setData({
              ddNumber: res.data.data,
              secondPayPassword: true,
              payFocus: true,
              whatpay: 1
            })
          }
        }
      })
    }
  },
  // zc支付
  zcrealyPay() {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/integral/payment',
      data: {
        orderCode: that.data.ddNumber,
        type: 'offLine',
        integralNum: that.data.needZc,
        merchantId: that.data.msg.id,
        password: that.data.pwd,
        xcxOpenId: app.globalData.xcxOpenId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code == 0) {
          let msg = {
            flag: 'zc',
            money: that.data.needZc,
            who: that.data.who
          }
          let str = JSON.stringify(msg)
          wx.navigateTo({
            url: '../paySuccess/paySuccess?jsonStr=' + str,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  // 星钻支付
  xzrearlyPay() {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/currency/payment',
      data: {
        orderCode: that.data.ddNumber,
        type: 'offLine',
        currencyNum: that.data.needXz,
        merchantId: that.data.msg.id,
        password: that.data.pwd,
        xcxOpenId: app.globalData.xcxOpenId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code == 0) {
          let msg = {
            flag: '星钻',
            money: that.data.needXz,
            who: that.data.who
          }
          let str = JSON.stringify(msg)
          wx.navigateTo({
            url: '../paySuccess/paySuccess?jsonStr=' + str,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  // 显示密码输入框
  showInputLayer: function () {
    this.setData({
      secondPayPassword: true,
      payFocus: true
    });
  },
  // 密码输入绑定和完成请求
  inputPwdNewTwo: function (e) {
    this.setData({
      pwd: e.detail.value
    });
    if (e.detail.value.length >= 6) {
      if (this.data.whatpay == 1) {
        this.zcrealyPay()
      } else if (this.data.whatpay == 2) {
        this.xzrearlyPay()
      }
    }
  },
  // 获取焦点
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
  // 隐藏输入框
  hidePayLayer: function () {
    this.setData({
      secondPayPassword: false,
      payFocus: false,
      pwd: ''
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (query) {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/user/getUserByOpenId',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            zc: res.data.data.integral ? res.data.data.integral : 0,
            xz: res.data.data.currency ? res.data.data.currency : 0
          })
        }
      }
    })
    const scene = decodeURIComponent(query.scene)
    wx.request({
      url: app.globalData.baseUrl + '/merchant/findMerchantById',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        merchantId: scene
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            msg: res.data.data
          })
        }
      }
    })
  }
})